import matplotlib.pyplot as plt
import numpy as np
import math

delta_T = 0.1  # 采样时间
T = 10  # 总时间
t_0 = np.arange(0, T, 0.01)  # 未采样前连续时间
Xk = 1.0  # 0阶真实信号
K = list(range(1, int(T / delta_T) + 1))  # 采样次数
wk = list(range(0, int(T / delta_T)))  # 干扰值
X_measure = list(range(0, int(T / delta_T)))  # 测量值
t = [0] * len(K)
e = [0] * len(K)  # 误差值
# 计算测量值
for i in range(0, len(K)):
    wk[i] = np.random.normal(loc=0.0, scale=1.0, size=None)
    X_measure[i] = Xk + wk[i]
for i in range(0, len(K)):
    t[i] = (K[i] - 1) * delta_T
xhat = (1 / len(K)) * sum(X_measure)

for i in range(0, len(K)):
    e[i] = X_measure[i] - xhat

plt.xlabel("Time(Sec)")
plt.ylabel("xhat")
plt.plot(t, X_measure, '-o', c='r', label='measures')
plt.axhline(y=xhat, c="k", label='estimates', ls="--", lw=2)
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.plot(t, e, '-o', label='error between estimates and measures')
plt.axhline(y=Xk - xhat, c="k", label='error between estimates and true', ls="--", lw=2)
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.axhline(y=xhat, c="r", label='estimates', ls="--", lw=2)
plt.axhline(y=Xk, c="g", label='true', ls="--", lw=2)
plt.xlim(0, T)
plt.ylim(0.8, 1.2)
plt.legend()
plt.show()
